<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="this.taskId != 0"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['backend:t_shop_bidding:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          v-if="this.taskId != 0"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['backend:t_shop_bidding:edit']"
        >修改</el-button>
      </el-col>-->

<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['backend:t_shop_bidding:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['backend:t_shop_bidding:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="t_shop_biddingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="表格ID" align="center" prop="id" />-->
<!--      <el-table-column label="任务id" align="center" prop="taskId" />-->
<!--      <el-table-column label="客户id" align="center" prop="customerId" />-->
      <el-table-column label="客户名称" align="center" prop="customerName" />
<!--      <el-table-column label="店铺ID" align="center" prop="shopId" />-->
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务描述" align="center" prop="taskInfo" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
<!--      <el-table-column label="竞标订单费用" align="center" prop="bidPrice"  />-->
<!--      <el-table-column label="竞标描述" align="center" prop="bidInfo"  />-->
      <el-table-column label="竞标订单费用" align="center" prop="bidPrice">
        <template slot-scope="scope">
          <span v-if="scope.row.shopId == currentShopId">{{ scope.row.bidPrice }}</span>
          <span v-if="scope.row.shopId != currentShopId">--</span>
        </template>
      </el-table-column>
      <el-table-column label="竞标描述" align="center" prop="bidInfo">
        <template slot-scope="scope">
          <span v-if="scope.row.shopId == currentShopId">{{ scope.row.bidInfo }}</span>
          <span v-if="scope.row.shopId != currentShopId">--</span>
        </template>
      </el-table-column>
      <el-table-column label="是否竞标结束" align="center" prop="isBidEnd">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.isBidEnd"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="this.taskId != 0">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.shopId == currentShopId && scope.row.isBidEnd != 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['backend:t_shop_bidding:edit']"
          >修改</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['backend:t_shop_bidding:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺竞标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="店铺ID" prop="shopId" v-show="false">
          <el-input v-model="form.shopId" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" readonly />
        </el-form-item>
        <el-form-item label="竞标订单费用" prop="bidPrice">
          <el-input v-model="form.bidPrice" placeholder="请输入竞标订单费用" />
        </el-form-item>
        <el-form-item label="竞标描述" prop="bidInfo">
          <el-input v-model="form.bidInfo" placeholder="请输入竞标描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listT_shop_bidding, getT_shop_bidding, delT_shop_bidding, addT_shop_bidding, updateT_shop_bidding } from "@/api/backend/t_shop_bidding"
import {getCurrentShop} from "@/api/backend/t_shop";

export default {
  name: "T_shop_bidding",
  dicts: ['yes_or_no'],
  props: {
    taskId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺竞标表格数据
      t_shop_biddingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前登录用户的店铺
      currentShopId: null,
      currentShopName: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        customerId: null,
        customerName: null,
        shopId: null,
        shopName: null,
        taskName: null,
        taskInfo: null,
        bidPrice: null,
        bidInfo: null,
        isBidEnd: null,
        orderId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bidPrice: [
          { required: true, message: "竞标订单费用不能为空", trigger: "blur" }
        ],
        bidInfo: [
          { required: true, message: "竞标描述不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList();
    this.getCurrentShop();
  },
  methods: {
    /** 查询店铺竞标列表 */
    getList() {
      this.loading = true;
      if (this.taskId != 0) {
        this.queryParams.taskId = this.taskId;
      }
      listT_shop_bidding(this.queryParams).then(response => {
        this.t_shop_biddingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        customerId: null,
        customerName: null,
        shopId: null,
        shopName: null,
        taskName: null,
        taskInfo: null,
        bidPrice: null,
        bidInfo: null,
        isBidEnd: null,
        orderId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 获取当前店铺信息 */
    getCurrentShop() {
      getCurrentShop().then(response => {
        this.currentShopId = response.data.id;
        this.currentShopName = response.data.shopName;
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺竞标";
      this.form.shopId = this.currentShopId;
      this.form.shopName = this.currentShopName;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getT_shop_bidding(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺竞标";
        this.form.shopId = this.currentShopId;
        this.form.shopName = this.currentShopName;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.form.taskId = this.taskId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateT_shop_bidding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addT_shop_bidding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除店铺竞标编号为"' + ids + '"的数据项？').then(function() {
        return delT_shop_bidding(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('backend/t_shop_bidding/export', {
        ...this.queryParams
      }, `t_shop_bidding_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
